#!/bin/bash

echo "=== 修复SQLite3 Native Binding问题 ==="

# 安装编译依赖
echo "安装编译依赖..."
yum update -y
yum groupinstall "Development Tools" -y
yum install -y python3 make gcc gcc-c++ sqlite-devel

# 清理sqlite3
echo "清理sqlite3模块..."
npm uninstall sqlite3
rm -rf node_modules/sqlite3

# 设置编译环境
export PYTHON=$(which python3)
export CXX=g++
export CC=gcc

# 重新安装sqlite3并强制编译
echo "重新安装sqlite3..."
npm install sqlite3@5.1.7 --build-from-source --registry=https://registry.npmmirror.com

# 如果还是失败，尝试预编译版本
if [ $? -ne 0 ]; then
    echo "编译失败，尝试预编译版本..."
    npm install sqlite3 --registry=https://registry.npmmirror.com
    
    # 手动下载预编译版本
    cd node_modules/sqlite3
    npm run install
    cd ../..
fi

echo "SQLite3修复完成！"

# 验证安装
echo "验证SQLite3安装..."
node -e "console.log(require('sqlite3'))" && echo "SQLite3可用" || echo "SQLite3仍有问题"